using System;
using NaughtyAttributes;
using UnityEngine;
using UnityEngine.UI;

namespace Gj
{
    public class RadarHelper : Graphic
    {
        public float radius;

        public int count;

        public float defaultRatio = 1;

        public float[] values;

        public void SetValue(float[] v)
        {
            values = v;
            SetVerticesDirty();
        }

        [Button]
        public void Make()
        {
            SetVerticesDirty();
        }

        protected override void OnPopulateMesh(VertexHelper vh)
        {
            vh.Clear();

            vh.AddVert(Vector3.zero, color, Vector2.zero);
            float a = 360 / count;
            for (var i = 0; i < count; i++)
            {
                var v = GetPosition(radius, i * a);
                var ratio = values != null && values.Length > i ? values[i] : defaultRatio;
                vh.AddVert(v * ratio, color, Vector2.zero);
                vh.AddTriangle(0, i + 1, i == count - 1 ? 1 : i + 2);
            }
        }

        private Vector3 GetPosition(float r, float a)
        {
            var x = (float)(r * Math.Cos(a * Math.PI / 180));
            var y = (float)(r * Math.Sin(a * Math.PI / 180));
            return new Vector3(x, y, 0);
        }
    }
}